package org.javaqq.face.main.base;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.LinearGradientPaint;
import java.awt.Paint;

import javax.swing.SwingConstants;
import javax.swing.plaf.UIResource;
import javax.swing.plaf.basic.BasicArrowButton;

import org.javaqq.face.util.JavaQQColor;


public class JavaQQArrowButton extends BasicArrowButton {

	public JavaQQArrowButton(int direction) {
		super(direction);
	}
	
	public void paint(Graphics g) {
	    Color origColor;
	    boolean isPressed, isEnabled;
	    int w, h, size;

            w = getSize().width;
            h = getSize().height;
	    origColor = g.getColor();
	    isPressed = getModel().isPressed();
	    isEnabled = isEnabled();

            g.setColor(JavaQQColor.getLightColor());
            g.fillRect(1, 1, w-2, h-2);
            
            if(this.getModel().isRollover()){
            	Paint f = null;
           	 if(this.getDirection()==SwingConstants.EAST){
           		f = new LinearGradientPaint(0.0f, 0.0f, 0.0f, this.getHeight(), new float[] { 0.0f, 1.0f },
    					new Color[] { JavaQQColor.getWrite(0),
           			JavaQQColor.getWrite(200) });
           	 }else if(this.getDirection()==SwingConstants.WEST){
           		f = new LinearGradientPaint(0.0f, 0.0f, 0.0f, this.getHeight(), new float[] { 0.0f, 1.0f },
    					new Color[] { JavaQQColor.getWrite(0),
           			JavaQQColor.getWrite(200) });
           	 }else if(this.getDirection()==SwingConstants.SOUTH){
           		f = new LinearGradientPaint(0.0f, 0.0f, this.getWidth(), 0.0f, new float[] { 0.0f, 1.0f },
    					new Color[] { JavaQQColor.getWrite(0),
           			JavaQQColor.getWrite(200) });
           	 }else if(this.getDirection()==SwingConstants.NORTH){
           		f = new LinearGradientPaint(0.0f, 0.0f, this.getWidth(), 0.0f, new float[] { 0.0f, 1.0f },
    					new Color[] { JavaQQColor.getWrite(0),
           			JavaQQColor.getWrite(200) });
           	 }
           	Graphics2D g2d = (Graphics2D) g;
           	 g2d.setPaint(f);
           	 g2d.fillRect(1, 1, w-2, h-2);
            }

            /// Draw the proper Border
	    if (getBorder() != null && !(getBorder() instanceof UIResource)) {
		paintBorder(g);
	    } else if (isPressed) {
		    	g.setColor(JavaQQColor.getDeepLineColor(150));
	            g.drawRect(0, 0, w-1, h-1);
            } else {
            	g.setColor(JavaQQColor.getGray(50));
	            g.drawRect(0, 0, w-1, h-1);
                /*g.setColor(SipColor.getMaxDeepLineColor(255));
                g.drawRect(0, 0, w, h);*/
            }

            // If there's no room to draw arrow, bail
            if(h < 5 || w < 5)      {
                g.setColor(origColor);
                return;
            }

            if (isPressed) {
                g.translate(1, 1);
            }

            // Draw the arrow
            size = Math.min((h - 4) / 3, (w - 4) / 3);
            size = Math.max(size, 2);
	    paintTriangle(g, (w - size) / 2, (h - size) / 2,
				size, direction, isEnabled);

            // Reset the Graphics back to it's original settings
            if (isPressed) {
                g.translate(-1, -1);
	    }
	    g.setColor(origColor);

        }

}
